home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / utils / refer-tools / tib-to-bibtex.shar / ttb.c < prev   
C/C++ Source or Header  |  1992-07-01  |  7KB  |  341 lines

  1. #define about "tibtobib 0.3, 1989 by uf"
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define maxlines 50
  7. #define linelen 256
  8. #define ntypes 8
  9. #define ntags 18
  10.  
  11. #define article 0
  12. #define conference 1
  13. #define inbook 2
  14. #define mastersthesis 3
  15. #define phdthesis 4
  16. #define techreport 5
  17. #define book 6
  18. #define misc 7
  19.  
  20. #define HELP 0
  21. #define NOREF 10
  22. #define NOBIB 11
  23. #define BIGITEM 12
  24. #define NOCONT1 13
  25. #define NOCONT2 14
  26. #define BIGLINE 15
  27. #define BADTAG 16
  28. #define MEMFULL 17
  29.  
  30. char RCSHeader[]="$Header: /home/tex/local/ttb.c,v 1.3 90/02/15 14:55:30 sk Rel $";
  31.  
  32. static char *inputtype[]={"ARTICLE","CONFERENCE","INBOOK","MASTERSTHESIS",
  33.                  "PHDTHESIS","TECHREPORT","BOOK","MISC"},
  34.   *recordtype[]={"AUTHOR","BOOKTITLE","ADDRESS","YEAR","EDITOR",
  35.            "PUBLISHER","JOURNAL","KEY","NUMBER","NOTE","PAGES",
  36.            "TYPE","SERIES","TITLE","VOLUME","COMMENT","ANNOTE",
  37.            "ABSTRACT"},
  38.   allowedtag[]={'A','B','C','D','E','I','J','K','N','O','P','R','S','T','V',
  39.           '%','x','X'},
  40.   organization[]="ORGANIZATION";
  41.  
  42.  
  43. char *item[maxlines],buffer[maxlines*linelen],
  44.   reffile[linelen],bibfile[linelen];
  45. int curline=0,type,ilines;
  46.  
  47. FILE *ref,*bib,*fopen();
  48.  
  49. main(argc,argv)
  50. int argc;
  51. char **argv;
  52. { int notready;
  53.  
  54.   if (argc != 2) {
  55.     fprintf(stderr,"%s, usage: ttb <filename>\n",about);
  56.     exit(HELP);
  57.   }
  58.  
  59.   strcpy(reffile,argv[1]);
  60.   if ((ref=fopen(reffile,"r")) == NULL) {
  61.     sprintf(reffile,"%s.ref",argv[1]);
  62.     if ((ref=fopen(reffile,"r")) == NULL) {
  63.       fprintf(stderr,"ttb: can open neither %s nor %s\n",argv[1],reffile);
  64.       exit(NOREF);
  65.     }
  66.   }
  67.  
  68.   if (!strcmp(argv[1]+strlen(argv[1])-4,".ref"))
  69.       argv[1][strlen(argv[1])-4]='\0';
  70.   sprintf(bibfile,"%s.bib",argv[1]);
  71.   if ((bib=fopen(bibfile,"w")) == NULL) {
  72.     fprintf(stderr,"ttb: can't open %s\n",bibfile);
  73.     exit(NOBIB);
  74.   }
  75.  
  76.   /* main loop */
  77.  
  78.   do {
  79.     notready=getitem();
  80.     type=classifyitem();
  81.     /* printitem(); */
  82.     putitem();
  83.   }
  84.   while(notready);
  85.  
  86.   /* exit */
  87.  
  88.   fclose(ref);
  89.   fclose(bib);
  90. }
  91.  
  92.  
  93. getitem()
  94. { char firstchar,secondchar;
  95.   int i,j,k,ok,ilm1,space,used,ignored=0;
  96.  
  97.   ilines = 0;
  98.   space = maxlines*linelen-1;
  99.   *item = buffer;
  100.  
  101.   while(!feof(ref)) {
  102.  
  103.     if (space<=0) {
  104.       fprintf(stderr,"ttb: %s, %d: buffer space exhausted\n",reffile,curline);
  105.       exit(MEMFULL);
  106.     }
  107.  
  108.     fgets(item[ilines++],space,ref);
  109.     ilm1 = ilines-1;
  110.     for (k=strlen(item[ilm1])-1; k>-1 && isspace(item[ilm1][k]); item[ilm1][k--]=0)
  111.       ;
  112.     /* printf("%s\n",item[ilm1]); */
  113.     curline++;
  114.  
  115.     if (ilines>maxlines) {
  116.       fprintf(stderr,"ttb: %s, line %d: too many lines for this item, line ignored\n",reffile,curline);
  117.       ilines--;
  118.       continue;
  119.     }
  120.  
  121.     if (strlen(item[ilm1]) == 0) {  /* blank line */
  122.       ilines--;
  123.       ignored=0;
  124.       break;
  125.     }
  126.  
  127.     if ((firstchar=item[ilm1][0]) != '%') {  /* continuation line */
  128.       if (ilines == 1) {
  129.     fprintf(stderr,"ttb: %s, line %d: leading continuation line ignored\n",reffile,curline);
  130.     ilines--;
  131.     continue;
  132.       }
  133.       if (ignored) {
  134.     fprintf(stderr,"ttb: %s, line %d: continuation line ignored\n",reffile,curline);
  135.     ilines--;
  136.     continue;
  137.       }
  138.  
  139.       ilines--;
  140.       *(item[ilines]-1) = '\n';
  141.       used = strlen(item[ilines])+1;
  142.       item[ilines] += used;
  143.       continue;
  144.     }
  145.  
  146.     secondchar = item[ilm1][1];
  147.     ok = 0;
  148.     for (i=0; i<ntags; i++)
  149.       if (secondchar == allowedtag[i]) {
  150.     ok = -1;
  151.     break;
  152.       }
  153.  
  154.     if (!ok) {
  155.       fprintf(stderr,"ttb: %s, line %d: field tag %c not allowed, line ignored\n",reffile,curline,secondchar);
  156.       ignored = -1;
  157.       ilines--;
  158.       continue;
  159.     }
  160.  
  161.     ignored = 0;
  162.     space -= (used = strlen(item[ilm1])+1);
  163.     item[ilines] = item[ilm1]+used;
  164.   }
  165.  
  166.   return(!feof(ref));
  167. }
  168.  
  169.  
  170. int classifyitem()
  171. { int p,type;
  172.  
  173.   if (test('J') && test('V'))
  174.       type = article;
  175.   else
  176.     if (test('B'))
  177.       type = inbook;
  178.     else
  179.       if (p=test('R'))
  180.     if (index(item[p-1],"thesis"))
  181.       if (index(item[p-1],"master"))
  182.         type = mastersthesis;
  183.       else
  184.         type = phdthesis;
  185.     else
  186.       type = techreport;
  187.       else
  188.     if (test('I'))
  189.       type = book;
  190.     else
  191.       type = conference;
  192.   
  193.   return(type);
  194. }
  195.  
  196.  
  197. index(s,t)
  198. char s[],t[];
  199. { int i,j,k;
  200.  
  201.   for (i=0; s[i]; i++) {
  202.     for (j=i,k=0; t[k] && tolower(s[j])==tolower(t[k]); j++,k++)
  203.       ;
  204.     if (t[k] == 0)
  205.       return(i+1);
  206.   }
  207.  
  208.   return(0);
  209. }
  210.  
  211.  
  212. int test(c)
  213. char c;
  214. { int i;
  215.  
  216.   for (i=0; i<ilines; i++)
  217.     if (item[i][1] == c)
  218.       return(i+1);
  219.  
  220.   return(0);
  221. }
  222.  
  223.  
  224. putitem()
  225. { char author[linelen],year[linelen],page[linelen],record[linelen],
  226.     editor[linelen],secondchar;
  227.   int i,j,k,p,first;
  228.  
  229.   if (p=test('A')) {
  230.     if ((i = index(item[p-1],",")) > 4) {
  231.       strcpy(author,item[p-1]+3);
  232.       author[i-4]=0;
  233.     }
  234.     else {
  235.       for (k=strlen(item[p-1]); k>2 && item[p-1][k] != ' ' && item[p-1][k] != '.'; k--)
  236.     ;
  237.       strcpy(author,item[p-1]+k+1);
  238.     }
  239.   }
  240.   else
  241.     strcpy(author,"Mr.X");
  242.  
  243.   if (p=test('D'))
  244.     strcpy(year,item[p-1]+strlen(item[p-1])-2);
  245.   else
  246.     strcpy(year,"??");
  247.  
  248.   if (p=test('P')) {
  249.     for (i=3; i<strlen(item[p-1]) && (isalpha(item[p-1][i]) && i==3 || isdigit(item[p-1][i])); page[i-3]=item[p-1][i], i++)
  250.       ;
  251.     page[i-3]=0;
  252.   }
  253.   else
  254.     strcpy(page,"0");
  255.  
  256.   if (p=test('K'))     /* user provides keyword */
  257.     fprintf(bib,"@%s{%s%c\n",inputtype[type],item[p-1]+3,ilines>0 ? ',' : ' ');
  258.   else
  259.     fprintf(bib,"@%s{%s%s:%s%c\n",inputtype[type],author,year,page,ilines>0 ? ',' : ' ');
  260.  
  261.   /* special cases: several authors, editors */
  262.  
  263.   first = !0;
  264.   strcpy(author,"");
  265.   if (test('A')) {
  266.     for (i=0; i<ilines; i++) {
  267.       if (item[i][1] == 'A') {
  268.     if (first)
  269.       first = 0;
  270.     else
  271.       strcat(author," and ");
  272.     strcat(author,item[i]+3);
  273.       }
  274.     }
  275.     fprintf(bib,"  %s = {%s}%c\n",recordtype[0],author,item[ilines-1][1] != 'A' ? ',' : ' ');
  276.   }
  277.   
  278.   first = !0;
  279.   strcpy(editor,"");
  280.   if (test('E')) {
  281.     for (i=0; i<ilines; i++) {
  282.       if (item[i][1] == 'E') {
  283.     if (first)
  284.       first = 0;
  285.     else
  286.       strcat(editor," and ");
  287.     strcat(editor,item[i]+3);
  288.       }
  289.     }
  290.     fprintf(bib,"  %s = {%s}%c\n",recordtype[4],editor,item[ilines-1][1] != 'E' ? ',' : ' ');
  291.   }
  292.  
  293.   /* all other fields */
  294.   
  295.   for (i=0; i<ilines; i++) {
  296.     secondchar = item[i][1];
  297.     if (secondchar == 'A' || secondchar == 'E')
  298.       continue;
  299.     
  300.     /* special case: %J in a conference */
  301.     
  302.     if (secondchar == 'J' && type == conference)
  303.       strcpy(record,organization);
  304.     
  305.     /* all other cases */
  306.     
  307.     else
  308.       for (j=0; j<ntags; j++)
  309.     if (secondchar == allowedtag[j]) {
  310.       strcpy(record,recordtype[j]);
  311.       break;
  312.     }
  313.     
  314.     fprintf(bib,"  %s = {%s}%c\n",record,item[i]+3,i<ilines-1 ? ',' : ' ');
  315.   }
  316.   
  317.   fprintf(bib,"}\n\n");
  318. }
  319.  
  320. option(argc,argv,c)
  321. char **argv,c;
  322. int argc;
  323. { int i,j;
  324.  
  325.   for (i=1; i<argc; i++)
  326.     if (argv[i][0] == '-')
  327.       for (j=1; j<strlen(argv[i]); j++)
  328.     if (argv[i][j] == c)
  329.       return(i);
  330.   return(0);
  331. }
  332.  
  333. printitem()
  334. { int i;
  335.  
  336.   for (i=0; i<ilines; i++)
  337.     printf("%s\n",item[i]);
  338.   printf("\nitem consisting of %d lines, type = %d\n\n",ilines,type);
  339. }
  340.  
  341.